package com.brillilab.service.core.team.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.team.TeamMemberMapper;
import com.brillilab.domain.enums.team.IsLeaderEnum;
import com.brillilab.domain.enums.team.TeamMemberStateEnum;
import com.brillilab.domain.po.team.TeamMember;
import com.brillilab.domain.vo.team.TeamMemberVo;
import com.brillilab.service.core.team.ITeamMemberService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 研究团队成员
 *
 * @author WuMenghao
 * @since 2019-03-02
 */
@Service
@Transactional
public class TeamMemberServiceImpl implements ITeamMemberService {

    @Resource
    private TeamMemberMapper teamMemberMapper;

    @Override
    public boolean insertBatch(List<TeamMember> teamMembers) {
        Assert.isTrue(teamMembers != null && teamMembers.size() > 0,"参数缺失！");
        return teamMemberMapper.insertBatch(teamMembers) > 0;
    }

    @Override
    public List<TeamMember> selectListByTeamIdAndState(Long teamId,Integer... state) {
        Assert.isTrue(teamId != null && state != null,"参数缺失！");
        return teamMemberMapper
                .selectList(new LambdaQueryWrapper<TeamMember>()
                        .eq(TeamMember::getTeamId,teamId)
                        .in(TeamMember::getState,state));
    }

    @Override
    public List<TeamMember> selectListByTeamId(Long teamId) {
        return teamMemberMapper
                .selectList(new LambdaQueryWrapper<TeamMember>()
                        .eq(TeamMember::getTeamId,teamId));
    }

    @Override
    public boolean updateBatch(List<TeamMember> teamMembers,Long teamId) {
        Assert.isTrue(teamMembers != null && teamMembers.size() > 0,"list can not be null or empty");
        return teamMemberMapper.updateBatch(teamMembers,teamId) > 0;
    }

    @Override
    public List<TeamMember> selectListByLabIdAndState(Long labId,Integer state) {
        Assert.isTrue(labId != null && state != null,"参数缺失！");
        return teamMemberMapper
                .selectList(new LambdaQueryWrapper<TeamMember>()
                        .eq(TeamMember::getLabId,labId)
                        .eq(TeamMember::getState,state));
    }

    @Override
    public List<TeamMemberVo> selectTeamMemberVoList(Long teamId,Integer state) {
        Assert.isTrue(teamId != null && state != null,"参数缺失！");
        return teamMemberMapper.selectTeamMemberVoList(teamId,state);
    }

    @Override
    public TeamMemberVo selectLeaderVo(Long teamId) {
        Assert.isTrue(teamId != null,"参数缺失！");
        return teamMemberMapper.selectLeader(teamId);
    }

    @Override
    public boolean updateTeamMemberState(Long teamId) {
        Assert.isTrue(teamId != null,"参数缺失！");
        return teamMemberMapper.updateTeamMemberState(teamId) > 0;
    }

    public List<TeamMember> selectAllTeamLabMemberId(Long labId) {
        Assert.isTrue(labId != null, "参数缺失！");
        return teamMemberMapper.selectList(
                new LambdaQueryWrapper<TeamMember>()
                        .eq(TeamMember::getLabId, labId)
                        .eq(TeamMember::getState, TeamMemberStateEnum.IN.getState())
        );
    }

    @Override
    public TeamMember selectLeader(Long teamId) {
        return teamMemberMapper.selectOne(
                new LambdaQueryWrapper<TeamMember>()
                        .eq(TeamMember::getTeamId,teamId)
                        .eq(TeamMember::getIsLeader,IsLeaderEnum.IS.getIsLeader())
                        .eq(TeamMember::getState,TeamMemberStateEnum.IN.getState()));
    }

    @Override
    public TeamMember selectOneByLabMemberIdAndState(Long teamId,Long labMemberId,Integer state) {
        return teamMemberMapper.selectOne(
                new LambdaQueryWrapper<TeamMember>()
                        .eq(TeamMember::getTeamId,teamId)
                        .eq(TeamMember::getLabMemberId,labMemberId)
                        .eq(TeamMember::getState,state));
    }

    @Override
    public boolean updateById(TeamMember newLeader) {
        newLeader.setUpdateTime(new Date());
        return teamMemberMapper.updateById(newLeader) > 0;
    }

    @Override
    public boolean getOutById(Long id) {
        TeamMember teamMember=teamMemberMapper.selectById(id);
        teamMember.setState(TeamMemberStateEnum.OUT.getState());
        teamMember.setIsLeader(IsLeaderEnum.NO.getIsLeader());
        teamMember.setUpdateTime(new Date());
        return teamMemberMapper.updateById(teamMember) > 0;
    }

    @Override
    public List<TeamMember> selectLeaderList(Long labId) {
        return teamMemberMapper.selectList(
                new LambdaQueryWrapper<TeamMember>()
                        .eq(TeamMember::getLabId,labId)
                        .eq(TeamMember::getState,TeamMemberStateEnum.IN.getState())
                        .eq(TeamMember::getIsLeader,IsLeaderEnum.IS.getIsLeader()));
    }

    @Override
    public TeamMember selectOneByLabIdAndLabMemberId(Long labId,Long labMemberId) {
        return teamMemberMapper.selectOne(
                new LambdaQueryWrapper<TeamMember>()
                        .eq(TeamMember::getLabId,labId)
                        .eq(TeamMember::getLabMemberId,labMemberId)
                        .eq(TeamMember::getState,TeamMemberStateEnum.IN.getState()));
    }

    @Override
    public TeamMember selectByLabMemberId(Long labMemberId){
        return teamMemberMapper.selectOne(new LambdaQueryWrapper<TeamMember>()
                .eq(TeamMember::getLabMemberId,labMemberId)
                .eq(TeamMember::getState, TeamMemberStateEnum.IN.getState()));
    }

    @Override
    public int insert(TeamMember teamMember) {
        return teamMemberMapper.insert(teamMember);
    }
}
